package com.woniuxy.woniubookmanager.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.woniubookmanager.entity.UrlPermission;
import com.woniuxy.woniubookmanager.result.ResponseResult;
import com.woniuxy.woniubookmanager.service.UrlPermissionService;
import com.woniuxy.woniubookmanager.utils.JwtUtils;
import com.woniuxy.woniubookmanager.vo.PermissionMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单和接口权限定义 前端控制器
 * </p>
 *
 * @author wugd
 * @since 2022-11-29 04:03:13
 */
@RestController
@RequestMapping("/urlPermission")
//@CrossOrigin
public class UrlPermissionController {

    @Autowired
    UrlPermissionService urlPermissionService ;

    @GetMapping("/getAllMenu")
    public ResponseResult<List<PermissionMenu>> getAllMenu(){
        ResponseResult<List<PermissionMenu>> result = null;
        try {
            List<PermissionMenu> list = urlPermissionService.getAllMenu();
            result = new ResponseResult<>(200,"ok",list);
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"查询失败");
            e.printStackTrace();
        }
        return result ;
    }

    @GetMapping("/getPermissionMenu")
    public ResponseResult<List<PermissionMenu>> getPermissionMenu(HttpServletRequest request){
        //获取用户名  用户名存储在 token 中 ，获取token  必须把token传递过来
        String token = request.getHeader("token");
        String account = JwtUtils.getAccount(token);

        ResponseResult<List<PermissionMenu>> result = null;
        try {
            if ("admin".equals(account)) {
                List<PermissionMenu> list = urlPermissionService.getAllMenu();
                result = new ResponseResult<>(200,"ok",list);
            }else {
                List<PermissionMenu> list = urlPermissionService.getPermissionMenuByUsername(account);
                result = new ResponseResult<>(200,"ok",list);
            }
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"查询失败");
            e.printStackTrace();
        }
        return result ;
    }

    @GetMapping("/permissionList")
    public ResponseResult<Page<UrlPermission>> list(@RequestParam(value = "now",defaultValue = "1") Integer now ,
                                                    @RequestParam(value = "size",defaultValue = "2")Integer size,
                                                    String pname){
        ResponseResult<Page<UrlPermission>> result = null;
        try {
            Page<UrlPermission> page = urlPermissionService.getPermissionByPage(now,size,pname);
            result = new ResponseResult<>(200,"OK",page);
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"查询失败");
            e.printStackTrace();
        }
        return result ;
    }

    @GetMapping("/getParentMenu")
    public ResponseResult<List<PermissionMenu>> getParentMenu(){
        ResponseResult<List<PermissionMenu>> result = null;
        try {
            List<PermissionMenu> list = urlPermissionService.getParentMenu();
            result = new ResponseResult<>(200,"OK",list);
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"查询失败");
            e.printStackTrace();
        }
        return result ;
    }

    @PostMapping("/addPermission")
    public ResponseResult<Void> addPermission(@RequestBody UrlPermission urlPermission){
        ResponseResult<Void> result = null;
        try {
            urlPermission.setInsertTime(new Date());
            //urlPermission.setUpdateTime(new Date());
            boolean save = urlPermissionService.save(urlPermission);
            result = new ResponseResult<>(200,"OK");
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"添加权限失败");
            e.printStackTrace();
        }
        return result ;
    }

    @GetMapping("/getSinglePermission/{id}")
    public ResponseResult<UrlPermission> getSinglePermission(@PathVariable("id") int id){
        ResponseResult<UrlPermission> result = null;
        try {
            UrlPermission permission = urlPermissionService.getById(id);
            result = new ResponseResult<>(200,"OK",permission);
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"查询失败");
            e.printStackTrace();
        }
        return result ;
    }

    @PutMapping("/modifyPermission")
    public ResponseResult<Void> modifyPermission(@RequestBody UrlPermission urlPermission){
        ResponseResult<Void> result = null;
        try {
            //根据 id 查询对象
            urlPermission.setUpdateTime(new Date());
            boolean save = urlPermissionService.updateById(urlPermission);
            result = new ResponseResult<>(200,"OK");
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"更新失败");
            e.printStackTrace();
        }
        return result ;
    }

    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> del(@PathVariable("id") Long id){
        ResponseResult<Void> result = null;
        try {
            urlPermissionService.removePermissionById(id);
            result = new ResponseResult<>(200,"OK");
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"删除失败");
            e.printStackTrace();
        }
        return result ;
    }

    @GetMapping("/getPermissionIdByRoleId/{id}")
    public ResponseResult<Long[]> getPermissionIdByRoleId(@PathVariable("id") Long id){
        ResponseResult<Long[]> result = null;
        try {
            Long[] pids = urlPermissionService.getPermissionIdByRoleId(id);
            result = new ResponseResult<>(200,"OK",pids);
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"查询失败");
            e.printStackTrace();
        }
        return result ;
    }
}

